#ifndef _PBVP_DATAMANAGER_H
#define _PBVP_DATAMANAGER_H

#include <assert.h>
#include <QList>
#include "datanode.h"

namespace PBVP {
	/// Central Manager of all data in the system
	class DataManager : public QObject {
		Q_OBJECT
	public:
		///
		DataManager();
		///
		virtual ~DataManager();
 
		///Add a data component into the manager
		/**
		 * @note The manager takes the control of the data node
		 */
		void addDataNode(DataNode* pDatanode);
		
		///Delete a data node of index id
		/** 
		 * @note The deleted data node is removed from the system
		 */
		bool deleteDataNode(int id);

		/// Delete a data node of the given pointer
		bool deleteDataNode(DataNode* pDataNode);

        /// Delete all nodes from the Manager
		void clear();

		/// Return the number of data nodes
		int numOfDataSets() const;

		/// Render data nodes in the manager
		void render();

		/// DataNode with given index
		DataNode* dataNodeAt(int i) const {
			assert(i < numOfDataSets());
			return m_DataList[i];
		}

		/// Find the index of a given datanode pointer
		/**
		 * @return -1 if the given pointer not found in the manager
		 */
		int findDataNode(DataNode* pDataNode) const;

		///
		const AxisAlignedBox& boundingBox() const {
			return m_BBox;
		}

		AxisAlignedBox& boundingBox() {
			return m_BBox;
		}
		/// Update bounding box based on data in the manager
		void updateBoundingBox();

		/// Get the current selected node
		DataNode* getCurrentNode() const {
			if(m_CurNodeId < 0) return NULL;
			return dataNodeAt(m_CurNodeId);
		}

	signals:
		void datasetChanged();

		void datasetAdded();

		void datasetDeleted();

		void rendererChanged();

	protected slots:
		void changRenderer();

		void changeDataset();

		void setCurrentNode(int id);

	protected:
		QList<DataNode *> m_DataList;
		// Bounding box of all data in the manager
		AxisAlignedBox m_BBox;

		int m_CurNodeId;
	};
}

#endif